package com.common.util;

import java.io.UnsupportedEncodingException;

public class ChangePassword {

	/**
	 * 密码加密
	 * 加密的密钥 
	 *@Value("${accountKey}")
	 *private String accountKey;
	 * 
	 * @param id	用户生成的uuid
	 * @param beforePasswd	用户输入的密码
	 * @return	加密过后的密码
	 */
	public static String changePasswd(String id,String beforePasswd,String accountKey){
		byte[] keyByte;
		String password = null;
		try {
			keyByte = DESedeUtil.getKeyByte(accountKey);
			String ivSpec = id.substring(0, 8);
			password=DESedeUtil.encryptMode(keyByte, beforePasswd, ivSpec);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return password;
	}
	
	/**
	 * 解密
	 * @author wuronglai
	 * @param id	用户id
	 * @param pwd	加密后的密码
	 * @param accountKey	密钥
	 * @return
	 */
	public static String decryptPasswd(String id,String pwd,String accountKey){
		String password = null;
		try {
			String ivSpec = id.substring(0, 8);
			byte[] keyByte = DESedeUtil.getKeyByte(accountKey);
			password = DESedeUtil.decryptMode(keyByte, DESedeUtil.hexStringToByte(pwd), ivSpec);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return password;
	}

	public static void main(String[] args) {
		System.out.println(decryptPasswd("5aac1afa09a34211bad97a5d25caa820","1FE970C04F9D9F29FAB4A9EBD8229D22","SLBGscs_SV1.0"));
	}


}
